package com.example.a16478091;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public EditText tx_username,tx_password;
    public Button btn_sign;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvent();
    }

    private void initView() {
        tx_username = findViewById(R.id.tx_username);
        tx_password = findViewById(R.id.tx_password);
        btn_sign = findViewById(R.id.btn_sign);
    }

    private void initEvent() {
        tx_password.setOnFocusChangeListener(new PasswordListener());
        btn_sign.setOnClickListener(new ButtonClick());
        tx_username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                String username = tx_username.getText().toString();
                if (!b){
                    if (TextUtils.isEmpty(username)) {
                        Toast t = Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_SHORT);
                        t.show();
                    }else {
                        Toast t = Toast.makeText(getApplicationContext(), username, Toast.LENGTH_SHORT);
                        t.show();
                    }
                }
            }
        });


    }
    class PasswordListener implements View.OnFocusChangeListener{
        @Override
        public void onFocusChange(View view, boolean b) {
            String password = tx_password.getText().toString();
            if (!b){
                if (TextUtils.isEmpty(password)) {
                    Toast t = Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT);
                    t.show();
                }else {
                    Toast t = Toast.makeText(getApplicationContext(), password, Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        }
    }

    class ButtonClick implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, MenuActivity.class));
        }
    }
}
